﻿using DotNetty.Common.Internal.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using System;
using System.IO;

namespace CCManger.Common
{
    public static class CCManagerHelper
    {
        static CCManagerHelper()
        {
            Configuration = new ConfigurationBuilder()
               .SetBasePath(ProcessDirectory)
               .AddJsonFile("appsettings.json", true, true)
               .Build();

            ClientSettings = new ClientSettings();
            Configuration.GetSection("ClientSettings").Bind(ClientSettings);

            ServerSettings = new ServerSettings();
            Configuration.GetSection("ServerSettings").Bind(ServerSettings);


            WinSettings = new WinSettings();
            Configuration.GetSection("WinSettings").Bind(WinSettings);
        }


        public static WinSettings WinSettings;
        public static ClientSettings ClientSettings;
        public static ServerSettings ServerSettings;
        public static IConfigurationRoot Configuration { get; }

        /// <summary>
        /// 获取目录
        /// </summary>
        public static string ProcessDirectory { get { return AppContext.BaseDirectory; } }

        /// <summary>
        /// 日志配置初始化(服务端)
        /// </summary>
        public static void InitServerSerilog()
        {
            // 配置 Serilog
            Log.Logger = new LoggerConfiguration()
                // 最小的日志输出级别
                .MinimumLevel.Debug()
                // 日志调用类命名空间如果以 Microsoft 开头，覆盖日志输出最小级别为 Information
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .Enrich.FromLogContext()
                // 配置日志输出到控制台
                .WriteTo.Console()
                // 配置日志输出到文件，文件输出到当前项目的 logs 目录下
                // 日记的生成周期为每天
                .WriteTo.File(Path.Combine("logs", @"serverlog.txt"), rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Information)
                // 创建 logger
                .CreateLogger();
            SetDotNettyLogger();
        }

        /// <summary>
        /// 日志配置初始化(客户端)
        /// </summary>
        public static void InitClientSerilog()
        {
            // 配置 Serilog
            Log.Logger = new LoggerConfiguration()
                // 最小的日志输出级别
                .MinimumLevel.Debug()
                // 日志调用类命名空间如果以 Microsoft 开头，覆盖日志输出最小级别为 Information
                .MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
                .Enrich.FromLogContext()
                // 配置日志输出到控制台
                .WriteTo.Console()
                // 配置日志输出到文件，文件输出到当前项目的 logs 目录下
                // 日记的生成周期为每天
                .WriteTo.File(Path.Combine("logs", @"clientlog.txt"), rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Information)
                // 创建 logger
                .CreateLogger();
            SetDotNettyLogger();
        }

        /// <summary>
        /// 配置DotNetty绑定到Serilog
        /// </summary>
        private static void SetDotNettyLogger()
        {
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddFilter("Microsoft", LogLevel.Debug)
                       .AddFilter("System", LogLevel.Debug)
                       .AddFilter("CCManger", LogLevel.Trace)
                       .AddSerilog();
                //.AddConsole();
            }
           );
            InternalLoggerFactory.DefaultFactory = loggerFactory;
        }
    }
}